package tt.dz.appserver.mvc.manage;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import tt.dz.appserver.util.Result;
import tt.dz.appserver.util.enums.Response;
import tt.dz.appserver.entity.model.AppVersion;
import tt.dz.appserver.entity.model.ResultMappingEntity;
import tt.dz.appserver.service.business.AppVersionService;

/**
 * App版本管理
 * @author USER001
 *
 */
@RestController
@RequestMapping("/manage/appVersion")
public class AppVersionManager {
	
	/**App版本管理*/
	@Resource
	AppVersionService appVersionService;
		
	/**
	 * App版本增加
	 * @param versioncode	版本号
	 * @param ostype		设备类型	
	 * @param description	版本说明
	 * @param releasetime	发布时间
	 * @param supportosver	支持os最低版本
	 * @param downloadurl	下载地址
	 **@param validflag		有效标志
	 * @return
	 */	
	@RequestMapping(value="/AppVersionAdd",produces={"application/json;charset=utf-8"})
	public String appversionadd(
			@RequestParam(value="versioncode") String versioncode,
			@RequestParam(value="ostype") Integer ostype,
			@RequestParam(value="description", required=false) String description,
			@RequestParam(value="releasetime") String releasetime,
			@RequestParam(value="supportosver") String supportosver,
			@RequestParam(value="downloadurl") String downloadurl) {
		
		//返回数据映射实体
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			
			//添加反馈
			JSONObject resultObject = appVersionService.appversionadd(versioncode, ostype, description, releasetime, 
					supportosver, downloadurl);
			
			String code = resultObject.getString("code");
			String msg = resultObject.getString("msg");
			
			if (Response.SUCCESS.getCode().equals(code)) {
				//返回参数拼接（删除resultObject对象中的code和msg再返回）
				resultObject.remove("code");
				resultObject.remove("msg");
				
				resultMappingEntity = Result.responseBody(code, msg, resultObject);
			}
			else {
				resultMappingEntity = Result.responseBody(code, msg);
			}			
			
		} catch(Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * App版本删除
	 * @param versioncode	版本号
	 * @param ostype		设备类型	
	 **@param description	版本说明
	 **@param releasetime	发布时间
	 **@param supportosver	支持os最低版本
	 **@param downloadurl	下载地址
	 **@param validflag		有效标志
	 * @return
	 */	
	@RequestMapping(value="/AppVersionDel",produces={"application/json;charset=utf-8"})
	public String appversiondel(
			@RequestParam(value="versioncode") String versioncode,
			@RequestParam(value="ostype") Integer ostype) {
		
		//返回数据映射实体
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			
			//添加反馈
			JSONObject resultObject = appVersionService.appversiondel(versioncode, ostype);
			
			String code = resultObject.getString("code");
			String msg = resultObject.getString("msg");
			
			if (Response.SUCCESS.getCode().equals(code)) {
				//返回参数拼接（删除resultObject对象中的code和msg再返回）
				resultObject.remove("code");
				resultObject.remove("msg");
				
				resultMappingEntity = Result.responseBody(code, msg, resultObject);
			}
			else {
				resultMappingEntity = Result.responseBody(code, msg);
			}			
			
		} catch(Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * App版本更新
	 * @param versioncode	版本号
	 * @param ostype		设备类型	
	 * @param description	版本说明
	 * @param releasetime	发布时间
	 * @param supportosver	支持os最低版本
	 * @param downloadurl	下载地址
	 **@param validflag		有效标志
	 * @return
	 */	
	@RequestMapping(value="/AppVersionUpdate",produces={"application/json;charset=utf-8"})
	public String appversionupdate(
			@RequestParam(value="versioncode") String versioncode,
			@RequestParam(value="ostype") Integer ostype,
			@RequestParam(value="description", required=false) String description,
			@RequestParam(value="releasetime", required=false) String releasetime,
			@RequestParam(value="supportosver", required=false) String supportosver,
			@RequestParam(value="downloadurl", required=false) String downloadurl) {
		
		//返回数据映射实体
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			
			//添加反馈
			JSONObject resultObject = appVersionService.appversionupdate(versioncode, ostype, description, 
					releasetime, supportosver, downloadurl);
			
			String code = resultObject.getString("code");
			String msg = resultObject.getString("msg");
			
			if (Response.SUCCESS.getCode().equals(code)) {
				//返回参数拼接（删除resultObject对象中的code和msg再返回）
				resultObject.remove("code");
				resultObject.remove("msg");
				
				resultMappingEntity = Result.responseBody(code, msg, resultObject);
			}
			else {
				resultMappingEntity = Result.responseBody(code, msg);
			}			
			
		} catch(Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * App版本查询
	 **@param versioncode	版本号
	 * @param ostype		设备类型	
	 **@param description	版本说明
	 **@param releasetime	发布时间
	 **@param supportosver	支持os最低版本
	 **@param downloadurl	下载地址
	 **@param validflag		有效标志
	 * @return
	 */	
	@RequestMapping(value="/AppVersionSelect",produces={"application/json;charset=utf-8"})
	public String appversionupdate(
			@RequestParam(value="ostype", required=false) Integer ostype) {
		System.out.println("ostype:"+ostype);
		//返回数据映射实体
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			
			//添加反馈
			List<AppVersion> resultObject = appVersionService.appversionselect(ostype);
			
			String code = Response.SUCCESS.getCode();
			String msg = Response.SUCCESS.getMsg();
			
			resultMappingEntity = Result.responseBody(code, msg, resultObject);
			
		} catch(Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
}
